package com.zw.admin.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.*;
import com.zw.admin.server.model.SysChan;
import com.zw.admin.server.service.ChanSerivce;
import com.zw.admin.server.utils.AbstractManager;
import com.zw.admin.server.utils.LogUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 类描述
 *
 * @author Mr.Wu
 * @create 2019-11-20
 */
@Service
public class SysChanServiceImpl implements ChanSerivce {

	private static DefaultAcsClient client = AbstractManager.getClient();

	/**
	 * 查询产品列表
	 *
	 * @param currentPage         指定从返回结果中的第几页开始显示  非必需
	 * @param pageSize            指定返回结果中每页显示的记录数量，最大值是200 非必需
	 * @param aliyunCommodityCode 产品描述 非必需
	 */
	@Override
	public QueryProductListResponse.Data QueryProductList(Integer currentPage, Integer pageSize, String aliyunCommodityCode) {

		QueryProductListResponse response = null;
		QueryProductListRequest request = new QueryProductListRequest();

		request.setAliyunCommodityCode(aliyunCommodityCode);
		request.setPageSize(pageSize);
		request.setCurrentPage(currentPage);

		try {
			response = client.getAcsResponse(request);

			if (response.getSuccess() != null && response.getSuccess()) {
				LogUtil.print("查询产品列表");
				LogUtil.print(JSON.toJSONString(response));
			} else {
				LogUtil.print("查询产品列表失败");
				LogUtil.error(JSON.toJSONString(response));
			}
			return response.getData();

		} catch (ClientException e) {
			e.printStackTrace();
			LogUtil.error("查询产品列表失败！" + JSON.toJSONString(response));
		}
		return null;
	}


	/**
	 * 删除产品
	 *
	 * @param productKey 产品PK 必需
	 */
	@Override
	public void deleteIotProduct(String productKey) {
		DeleteProductResponse response = null;

		DeleteProductRequest request = new DeleteProductRequest();
		request.setProductKey(productKey);

		try {
			response = client.getAcsResponse(request);

			if (response.getSuccess() != null && response.getSuccess()) {
				LogUtil.print("删除产品");
				LogUtil.print(JSON.toJSONString(response));
			} else {
				LogUtil.print("删除产品失败");
				LogUtil.error(JSON.toJSONString(response));
			}

		} catch (ClientException e) {
			e.printStackTrace();
			LogUtil.error("删除产品失败！");
		}
	}


	@Override
	public Map saveProduct(SysChan sysChan) {
		Map map = new HashMap();
		CreateProductResponse response = null;
		CreateProductRequest request = new CreateProductRequest();
		request.setProductName(sysChan.getProductName());
		request.setNodeType(0);
		request.setAliyunCommodityCode("iothub_senior");
		request.setDataFormat(1);
		request.setCategoryId(null);
		request.setDescription(null);
		request.setProtocolType("customize");
		request.setNetType("WIFI");
		request.setAuthType("secret");
		try {
			response = client.getAcsResponse(request);

			if (response.getSuccess() != null && response.getSuccess()) {
				LogUtil.print("创建产品成功");
				LogUtil.print(JSON.toJSONString(response));
				map.put("success",true);

			} else {
				LogUtil.print("创建产品失败");
				LogUtil.error(JSON.toJSONString(response));
				map.put("success",false);
				map.put("msg",response.getErrorMessage());
			}
		} catch (ClientException e) {
			e.printStackTrace();
			LogUtil.error("创建产品失败！" + JSON.toJSONString(response));
			map.put("success",false);
			map.put("msg",response.getErrorMessage());
		}
		return map;
	}


	/**
	 * 查询产品详细信息
	 *
	 * @param productKey 产品PK 必需
	 */
	@Override
	public QueryProductResponse.Data queryProduct(String productKey) {
		QueryProductResponse response = null;

		QueryProductRequest request = new QueryProductRequest();
		request.setProductKey(productKey);

		try {
			response = client.getAcsResponse(request);

			if (response.getSuccess() != null && response.getSuccess()) {
				LogUtil.print("查询产品详细信息");
				LogUtil.print(JSON.toJSONString(response));
			} else {
				LogUtil.print("查询产品失败");
				LogUtil.error(JSON.toJSONString(response));
			}
			return response.getData();

		} catch (ClientException e) {
			e.printStackTrace();
			LogUtil.error("查询产品失败！" + JSON.toJSONString(response.getData()));
		}
		return null;
	}

	@Override
	public UpdateProductResponse updateProduct(SysChan sysChan) {
		UpdateProductResponse updateProductResponse = null;
		UpdateProductRequest updateProductRequest = new UpdateProductRequest();
		updateProductRequest.setProductKey(sysChan.getProductKey());
		updateProductRequest.setProductName(sysChan.getProductName());
		try {
			updateProductResponse = client.getAcsResponse(updateProductRequest);

			if (updateProductResponse.getSuccess() != null && updateProductResponse.getSuccess()) {
				LogUtil.print("修改产品");
				LogUtil.print(JSON.toJSONString(updateProductResponse));
			} else {
				LogUtil.print("修改产品失败");
				LogUtil.error(JSON.toJSONString(updateProductResponse));
			}

		} catch (ClientException e) {
			e.printStackTrace();
			LogUtil.error("修改产品失败！" + JSON.toJSONString(updateProductResponse));
		}
		return  updateProductResponse;
	}
}
